package com.m.crm.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.m.crm.cache.RedisCache;
import com.m.crm.service.MailService;
import com.m.crm.util.MailUtil;
import com.m.crm.util.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by yugoh on 2017/5/26.
 */
@Service
public class MailServiceImpl implements MailService {
    private static final boolean SECURITY_CODE_REPLY = false;
    private static final String SECURITY_CODE_SUBJECT = "邮箱校验码";
    private static final String SECURITY_CODE_TAG_NAME = "邮箱校验码";
    private static final int SECURITY_CODE_SIZE = 8;
    private final static int SECURITY_CODE_CAHCE_TIME = 60 * 60;

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private final RedisCache cache;

    @Autowired
    public MailServiceImpl(RedisCache cache) {
        this.cache = cache;
    }

    @Override
    public void sendSecurityCode(String toAddress) throws ClientException {
        String securityCode = RandomUtil.generateRandom(SECURITY_CODE_SIZE);
        String htmlBody = "<center><table align='center' border='0' cellpadding='0' cellspacing='0' id='bodyTable' " +
                "style='background: rgb(255, 255, 255);margin-top: 30px;' width='100%'><tbody><tr><td align='center' " +
                "id='bodyCell' valign='top'><table border='0' cellpadding='0' cellspacing='0' id='templateContainer' " +
                "style='background: rgb(250, 250, 250);' width='700'><tbody><tr><td align='center' valign='top'>" +
                "<table border='0' cellpadding='0' cellspacing='0' id='templatePreheader' width='100%'><tbody><tr>" +
                "<td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' " +
                "class='templateContainer' width='700'><tbody><tr><td class='preheaderContainer tpl-container " +
                "dragTarget' data-container='preheader' valign='top'><div class='tpl-block tpl-image' " +
                "style='margin-top: 0px; margin-bottom: 0px; border: 0px solid rgb(26, 188, 156); " +
                "border-radius: 0px;'><div data-attach-point='containerNode'><table border='0' cellpadding='0' " +
                "cellspacing='0' class='imageBlock' width='100%'><tbody class='imageBlockOuter'><tr>" +
                "<td class='imageBlockInner' valign='top'><table align='left' border='0' cellpadding='0' " +
                "cellspacing='0' class='imageContentContainer' width='100%'><tbody><tr><td align='center' " +
                "class='imageContent' style='border: 1px solid rgb(238, 238, 238);padding:10px 20px; " +
                "text-align: left; border-bottom: 1px solid rgb(238, 238, 238);' valign='top'>" +
                "<img src='http://d2a3efkommrv9.cloudfront.net/mabofx/assets/images/logo/logo.png' " +
                "width='120px'></td></tr></tbody></table></td></tr></tbody></table></div></div></td></tr>" +
                "</tbody></table></td></tr></tbody></table></td></tr><tr><td align='center' valign='top'>" +
                "<table border='0' cellpadding='0' cellspacing='0' id='templateHeader' width='100%'><tbody><tr>" +
                "<td align='center' valign='top'><table border='0' cellpadding='0' cellspacing='0' " +
                "class='templateContainer' width='700'><tbody><tr>" +
                "<td class='headerContainer tpl-container dragTarget' data-container='header' valign='top'>" +
                "<div class='block tpl-block text-block' style='margin-top: 0px; margin-bottom: 0px; " +
                "border-radius: 0px;'><div data-attach-point='containerNode'><table border='0' cellpadding='0' " +
                "cellspacing='0' class='textBlock' width='100%'><tbody class='textBlockOuter'>" +
                "<tr><td class='textBlockInner' valign='top'><table align='left' border='0' cellpadding='0' " +
                "cellspacing='0' class='textContentContainer' width='700'><tbody><tr><td align='center' " +
                "class='textContent' style='border: 1px solid rgb(238, 238, 238);padding: 10px 25px; " +
                "text-align: left; background: url(bbimg.png) no-repeat;' valign='top'><div style='box-sizing: " +
                "border-box; margin: 0px 0px 10px;font-size: 10px;color: #8c8c8c; " +
                "font-family: 'Microsoft YaHei', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;'>" +
                "<p> 尊敬的用户 您好：</p><div style='margin:56px 160px 80px 180px;line-height: 30px;'>" +
                "<p>您此次操作的验证码为<span style='color: #50b7f1;'>" +
                securityCode +
                "</span>" +
                "（1 小时内有效），请在页面提交验证码完成验证，工作人员绝不会索取验证码，请勿告知他人，如有问题，请联系客服人员。</p>" +
                "</div><a href='' style='color: #50b7f1;text-decoration: none;'>点击查看详情</a>" +
                "<p style='margin: 46px 0 18px;'>此致</p><p>MAIBO UNIVER 团队</p></div></td></tr></tbody></table>" +
                "</td></tr></tbody></table></div></div><div class='' style='font-size: 10px;padding: 16px 0;" +
                "background: #FFFFFF;padding-right: 20px;'><p style='text-align: right;color: #50b7f1;'><a href='' " +
                "style='color: #50b7f1;'>技术支持</a><a href='' style='color: #50b7f1;'>条款与条件</a>" +
                "<a href='' style='color: #50b7f1;'>客户隐私政策</a></p></div><div class='' style='background: #FFFFFF;" +
                "text-align: center;font-size: 12px;color: #8c8c8c;float: left;'><img src='./bbimg.png'/>" +
                "<p style='color: #747474;'>此条为系统邮件，请勿直接回复。</p><p>Copyright  2017 | xxxx  保留所有权利</p>" +
                "</div></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr>" +
                "</tbody></table></center>";
        MailUtil.send(SECURITY_CODE_REPLY, toAddress, SECURITY_CODE_SUBJECT, SECURITY_CODE_TAG_NAME, htmlBody);
        String cacheKey = RedisCache.CAHCENAME + "|securityCode|" + toAddress;
        cache.putCacheWithExpireTime(cacheKey, securityCode, SECURITY_CODE_CAHCE_TIME);
    }
}
